/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.detail;

import com.siro.pm.I18nManager;
import com.siro.pm.model.Developer;
import com.siro.pm.model.provider.DeveloperProvider;
import com.siro.pm.ui.main.DevelopersPage;
import com.siro.pm.utils.UUIDGenerator;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since DeveloperDetailPage.java v1.0
 */
public class DeveloperDetailPage extends VerticalLayout {
	private static final long serialVersionUID = 6225106255250310462L;

	private Window window;
	private DevelopersPage developersPage;
	private Developer developer;

	private TextField name;
	private PasswordField password;
	private ComboBox role;
	private TextField postName;
	private TextField phoneNum;
	private TextField mailAddr;
	private TextField orgName;
	private Button button_Save;

	public DeveloperDetailPage() {
	}

	public void createContents() {
		setMargin(true);
		setSpacing(true);

		// base info
		GridLayout developerBaseInfo = new GridLayout();
		developerBaseInfo.setStyleName("h2");
		developerBaseInfo.setCaption(I18nManager
				.getMessage("developer.base.info"));
		developerBaseInfo.setColumns(2);
		developerBaseInfo.setMargin(true);
		developerBaseInfo.setSpacing(true);
		developerBaseInfo.setWidth("100%");

		name = new TextField(DeveloperProvider.DEVELOPER_PROPERTY_NAME);
		name.setWidth("100%");
		developerBaseInfo.addComponent(name);

		password = new PasswordField(
				DeveloperProvider.DEVELOPER_PROPERTY_PASSWORD);
		password.setWidth("100%");
		developerBaseInfo.addComponent(password);

		role = new ComboBox(DeveloperProvider.DEVELOPER_PROPERTY_ROLE);
		String[] statusData = DeveloperProvider.DEVELOPER_ROLE_DATA;
		for (int i = 0; i < statusData.length; i++) {
			role.addItem(statusData[i]);
		}
		role.setWidth("100%");
		role.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
		role.setImmediate(true);
		developerBaseInfo.addComponent(role);

		postName = new TextField(DeveloperProvider.DEVELOPER_PROPERTY_POSTNAME);
		postName.setWidth("100%");
		developerBaseInfo.addComponent(postName);

		phoneNum = new TextField(DeveloperProvider.DEVELOPER_PROPERTY_PHONENUM);
		phoneNum.setWidth("100%");
		developerBaseInfo.addComponent(phoneNum);

		mailAddr = new TextField(DeveloperProvider.DEVELOPER_PROPERTY_MAILADDR);
		mailAddr.setWidth("100%");
		developerBaseInfo.addComponent(mailAddr);

		orgName = new TextField(DeveloperProvider.DEVELOPER_PROPERTY_ORGNAME);
		orgName.setWidth("100%");
		developerBaseInfo.addComponent(orgName);

		// buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setMargin(true);
		buttons.setSpacing(true);
		buttons.setWidth("100%");

		button_Save = new Button(I18nManager.getMessage("button.save"));
		button_Save.setWidth(50, Sizeable.UNITS_PIXELS);
		button_Save.addListener(new SaveClickListener());
		buttons.addComponent(button_Save);
		buttons.setComponentAlignment(button_Save, Alignment.MIDDLE_RIGHT);

		addComponent(developerBaseInfo);
		addComponent(buttons);

		initData();
	}

	private void initData() {
		if (developer != null) {
			name.setValue(developer.getName());
			password.setValue(developer.getPassword());
			role.select(developer.getRole());
			postName.setValue(developer.getPostName());
			phoneNum.setValue(developer.getPhoneNum());
			mailAddr.setValue(developer.getMailAddr());
			orgName.setValue(developer.getOrgName());
		}
	}

	class SaveClickListener implements ClickListener {
		private static final long serialVersionUID = -2851840644753744125L;

		public void buttonClick(ClickEvent event) {
			boolean update = true;
			if (developer == null) {
				developer = new Developer();
				developer.setId(UUIDGenerator.newUUID());
				update = false;
			}
			developer.setName((String) name.getValue());
			developer.setPassword((String) password.getValue());
			developer.setRole((String) role.getValue());
			developer.setPostName((String) postName.getValue());
			developer.setPhoneNum((String) phoneNum.getValue());
			developer.setMailAddr((String) mailAddr.getValue());
			developer.setOrgName((String) orgName.getValue());
			if (update) {
				Developer.updateDeveloper(developer);
			} else {
				Developer.insertDeveloper(developer);
			}
			developersPage.developersTable
					.setContainerDataSource(DeveloperProvider
							.getDevelopersData(developersPage.getProject()
									.getDevelopers()));
			(window.getParent()).removeWindow(window);
		}
	}

	public Window getDeveloperWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}

	public DevelopersPage getDevelopersPage() {
		return developersPage;
	}

	public void setDevelopersPage(DevelopersPage developersPage) {
		this.developersPage = developersPage;
	}

	public Developer getDeveloper() {
		return developer;
	}

	public void setDeveloper(Developer developer) {
		this.developer = developer;
	}
}
